package com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyAdapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.diudiutechnology.www.yadiudiu.util.utils.Sundry;
import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity.CommodityManagementActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.CommodityManagementModel;
import com.diudiutechnology.www.yadiudiu.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/8/24.
 */

public class CommodityManagementAdapter extends BaseAdapter {

    private Context mContext;

    List<CommodityManagementModel> model = new ArrayList<>();
    List<Integer> listint = new ArrayList<>();
    String merchanttype = "";//类型

    public CommodityManagementAdapter(Context context, String merchanttype) {
        this.mContext = context;
        this.merchanttype = merchanttype;
    }

    @Override
    public int getCount() {
        return model.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void addItem(List<CommodityManagementModel> model, List<Integer> listint) {
        this.listint = listint;
        this.model = model;
        notifyDataSetChanged();
    }
    public void adddeleting(){

        model.clear();
        notifyDataSetChanged();
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.itme_commodity_management, null);
            holder.itme_time = (TextView) convertView
                    .findViewById(R.id.itme_time);
            holder.item_site = (TextView) convertView
                    .findViewById(R.id.item_site);
            holder.itme_justification = (TextView) convertView
                    .findViewById(R.id.itme_justification);
            holder.positioning_text = (TextView) convertView
                    .findViewById(R.id.positioning_text);
            holder.item_money = (TextView) convertView
                    .findViewById(R.id.item_money);
            holder.item_title = (TextView) convertView
                    .findViewById(R.id.item_title);
            holder.itme_type = (LinearLayout) convertView
                    .findViewById(R.id.itme_type);
            holder.switch1 = (LinearLayout) convertView
                    .findViewById(R.id.switch1);
            holder.rejected_deleting_layout = (LinearLayout) convertView
                    .findViewById(R.id.rejected_deleting_layout);
            holder.rejected = (TextView) convertView
                    .findViewById(R.id.rejected);
            holder.modification = (TextView) convertView
                    .findViewById(R.id.modification);
            holder.deleting = (TextView) convertView
                    .findViewById(R.id.deleting);
            holder.line = (TextView) convertView
                    .findViewById(R.id.line);
            holder.typename = (TextView) convertView
                    .findViewById(R.id.typename);
            holder.switch1img = (ImageView) convertView
                    .findViewById(R.id.switch1img);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final CommodityManagementModel bmodel = model.get(position);

        if (merchanttype.equals("3") || merchanttype.equals("5")) {//干洗商家

            if (listint.get(position) == position) {
                holder.itme_type.setVisibility(View.VISIBLE);
                holder.typename.setText(bmodel.getNametype());
                System.out.println("stringmd5result数--------" + listint.get(position)+"position="+position);
            } else {
                System.out.println("stringmd5result数--------" + listint.get(position)+"position="+position);
                holder.itme_type.setVisibility(View.GONE);
            }

        }

        holder.item_title.setText(bmodel.getName());
        holder.item_site.setText(bmodel.getIntroduction());
        holder.itme_time.setText(Sundry.getStrTime(2, bmodel.getAdd_time()));
        if (merchanttype.equals("1")) {//买房商家
            holder.item_money.setText(Sundry.getmoyedouble1s(bmodel.getPrice()));
        } else  if (merchanttype.equals("8")) {//家政商家
            holder.item_money.setText("¥"+bmodel.getPrice()+bmodel.getServicetime());
        }else {
            holder.item_money.setText(Sundry.getmoyedouble1(bmodel.getPrice()));
        }

        holder.positioning_text.setText(Sundry.getorder(bmodel.getOrder()));
        if (merchanttype.equals("1")||merchanttype.equals("2") || merchanttype.equals("9")) {//购车商家 //洗车商家 //买房商家
            if (bmodel.getAudit().equals("2")) {//审核中
                holder.switch1.setVisibility(View.GONE);
                holder.itme_justification.setText("审核中");
                holder.itme_justification.setVisibility(View.VISIBLE);
                holder.rejected_deleting_layout.setVisibility(View.GONE);
            } else if (bmodel.getAudit().equals("3")) {//审核通过
                holder.switch1.setVisibility(View.VISIBLE);
                if (merchanttype.equals("1")) {//买房商家
                    if (bmodel.getIsdisplay().equals("2")) {//下架状态
                        holder.switch1img.setImageResource(R.mipmap.toggle_btn_on);
                    } else {
                        holder.switch1img.setImageResource(R.mipmap.toggle_btn_off);
                    }
                    holder.rejected.setText("户型管理");
                    holder.rejected.setVisibility(View.VISIBLE);
                } else if (merchanttype.equals("2")) {//购车商家
                    if (bmodel.getIsdisplay().equals("2")) {//下架状态
                        holder.switch1img.setImageResource(R.mipmap.toggle_btn_off);
                    } else {
                        holder.switch1img.setImageResource(R.mipmap.toggle_btn_on);
                    }
                    if (bmodel.getSelected().equals("1")) {
                        holder.rejected.setText("推荐");
                    } else {
                        holder.rejected.setText("已推荐");
                    }
                    holder.rejected.setVisibility(View.VISIBLE);
                } else {
                    if (bmodel.getIsdisplay().equals("2")) {//下架状态
                        holder.switch1img.setImageResource(R.mipmap.toggle_btn_off);
                    } else {
                        holder.switch1img.setImageResource(R.mipmap.toggle_btn_on);
                    }
                    holder.rejected.setVisibility(View.INVISIBLE);
                }
                holder.itme_justification.setVisibility(View.GONE);
                holder.rejected_deleting_layout.setVisibility(View.VISIBLE);

            } else if (bmodel.getAudit().equals("4")) {//驳回
                holder.switch1.setVisibility(View.GONE);
                holder.itme_justification.setText("被驳回");
                holder.itme_justification.setVisibility(View.VISIBLE);
                holder.rejected_deleting_layout.setVisibility(View.VISIBLE);
                holder.rejected.setVisibility(View.VISIBLE);
            } else if (bmodel.getAudit().equals("5")) {//回收
                holder.switch1.setVisibility(View.GONE);
                holder.itme_justification.setText("被回收");
                holder.itme_justification.setVisibility(View.VISIBLE);
                holder.rejected_deleting_layout.setVisibility(View.VISIBLE);
                holder.rejected.setVisibility(View.VISIBLE);
            } else {
                holder.switch1.setVisibility(View.GONE);
                holder.itme_justification.setText("被回收");
                holder.itme_justification.setVisibility(View.VISIBLE);
                holder.rejected_deleting_layout.setVisibility(View.VISIBLE);
                holder.rejected.setVisibility(View.VISIBLE);
            }
        } else {//干洗商家 //果蔬商家 //家政商家
            if (bmodel.getAudit().equals("4")) {//审核中
                holder.switch1.setVisibility(View.GONE);
                holder.itme_justification.setText("审核中");
                holder.itme_justification.setVisibility(View.VISIBLE);
                holder.rejected_deleting_layout.setVisibility(View.GONE);
            } else if (bmodel.getAudit().equals("2")) {//审核通过
                holder.switch1.setVisibility(View.VISIBLE);
                if (bmodel.getIsdisplay().equals("3")) {//下架状态
                    holder.switch1img.setImageResource(R.mipmap.toggle_btn_off);
                } else {
                    holder.switch1img.setImageResource(R.mipmap.toggle_btn_on);
                }
                if (merchanttype.equals("1")) {//买房商家
                    holder.itme_justification.setText("户型管理");
                    holder.rejected.setVisibility(View.VISIBLE);
                } else if (merchanttype.equals("2")) {//购车商家
                    if (bmodel.getSelected().equals("1")) {
                        holder.itme_justification.setText("推荐");
                    } else {
                        holder.itme_justification.setText("已推荐");
                    }
                    holder.rejected.setVisibility(View.VISIBLE);
                } else {
                    holder.rejected.setVisibility(View.INVISIBLE);
                }
                holder.itme_justification.setVisibility(View.GONE);
                holder.rejected_deleting_layout.setVisibility(View.VISIBLE);

            } else if (bmodel.getAudit().equals("3")) {//驳回
                holder.switch1.setVisibility(View.GONE);
                holder.itme_justification.setText("被驳回");
                holder.itme_justification.setVisibility(View.VISIBLE);
                holder.rejected_deleting_layout.setVisibility(View.VISIBLE);
                holder.rejected.setVisibility(View.VISIBLE);
            } else if (bmodel.getAudit().equals("5")) {//回收
                holder.switch1.setVisibility(View.GONE);
                holder.itme_justification.setText("被回收");
                holder.itme_justification.setVisibility(View.VISIBLE);
                holder.rejected_deleting_layout.setVisibility(View.VISIBLE);
                holder.rejected.setVisibility(View.VISIBLE);
            } else {
                holder.switch1.setVisibility(View.GONE);
                holder.itme_justification.setText("被回收");
                holder.itme_justification.setVisibility(View.VISIBLE);
                holder.rejected_deleting_layout.setVisibility(View.VISIBLE);
                holder.rejected.setVisibility(View.VISIBLE);
            }
        }

        if ((model.size() - 1) == position) {
            holder.line.setVisibility(View.VISIBLE);
        } else {
            holder.line.setVisibility(View.GONE);
        }
        holder.modification.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {//修改
                ((CommodityManagementActivity) mContext).processIntent("1", bmodel.getId(), position);
            }
        });
        holder.rejected.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {//驳回

                    if (merchanttype.equals("1")) {//买房商家
                        ((CommodityManagementActivity) mContext).processmodel(bmodel.getId());

                    } else if (merchanttype.equals("2")) {//购车商家

                    } else {//驳回
                    ((CommodityManagementActivity) mContext).processreject();
                }
            }
        });
        holder.deleting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {//修改
                ((CommodityManagementActivity) mContext).processdeleting(bmodel.getId());
            }
        });
        holder.switch1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ((CommodityManagementActivity) mContext).processshelves(bmodel.getId(), bmodel.getIsdisplay());
            }
        });
        return convertView;
    }

    class ViewHolder {
        TextView positioning_text;//数量
        TextView itme_time; //时间
        TextView item_site; //内容
        TextView itme_justification; //理由
        TextView item_money; //钱
        TextView item_title; //标题
        LinearLayout itme_type; //类型
        LinearLayout switch1; //开关
        ImageView switch1img;

        LinearLayout rejected_deleting_layout;
        TextView rejected; //驳回
        TextView modification; //修改
        TextView deleting;//删除
        TextView line;
        TextView typename;//类名

    }
}

